package fn;

public class Power extends Function {

	private static final long serialVersionUID = -3476552541535755000L;

	private Function[] child;

	private Power(Function f0, Function f1) {
		child = new Function[] {
				f0, f1 };
	}

	public static Function create(Function f0, Function f1) {
		return new Power(f0, f1);
	}

	@Override
	protected Function[] child() {
		return child;
	}

	@Override
	public double evaluate(double sub) {
		return Math.pow(child[0].evaluate(sub), child[1].evaluate(sub));
	}

	@Override
	public String prefix() {
		return "{";
	}

	@Override
	public String infix() {
		return " }^{ ";
	}

	@Override
	public String postfix() {
		return "}";
	}

	@Override
	protected int precedence() {
		return 3;
	}

	@Override
	protected boolean withPar() {
		return false;
	}

	public Function derivative() {
		Function out = null;
		Function f = child[0];
		Function g = child[1];
		out = Multiply.create(this, Add.create(Multiply.create(f.derivative(), Divide.create(g, f)), Multiply.create(g.derivative(), Ln.create(f))));
		return out;
	}
}
